package cn.hj.domain.activity.service.quota.chain.impl;

import cn.hj.domain.activity.model.entity.ActivityCountEntity;
import cn.hj.domain.activity.model.entity.ActivityEntity;
import cn.hj.domain.activity.model.entity.ActivitySkuEntity;
import cn.hj.domain.activity.model.vo.ActivityStateVO;
import cn.hj.domain.activity.service.quota.chain.AbstractActivityChain;
import cn.hj.types.enums.ResponseCode;
import cn.hj.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 活动规则过滤（日期状态）
 */
@Component("activity_base_action")
@Slf4j
public class ActivityBaseActionChain extends AbstractActivityChain {

    @Override
    public boolean action(ActivitySkuEntity activitySkuEntity, ActivityCountEntity activityCountEntity, ActivityEntity activityEntity) {
        log.info("活动规则过滤（日期状态）校验开始。sku:{} activityId:{}", activitySkuEntity.getSku(), activityEntity.getActivityId());
        // 校验；活动状态
        if (!ActivityStateVO.open.equals(activityEntity.getState())) {
            throw new AppException(ResponseCode.ACTIVITY_STATE_ERROR.getCode(), ResponseCode.ACTIVITY_STATE_ERROR.getInfo());
        }
        // 校验；活动日期「开始时间 <- 当前时间 -> 结束时间」
        Date currentDate = new Date();
        if (activityEntity.getBeginDateTime().after(currentDate) || activityEntity.getEndDateTime().before(currentDate)) {
            throw new AppException(ResponseCode.ACTIVITY_DATE_ERROR.getCode(), ResponseCode.ACTIVITY_DATE_ERROR.getInfo());
        }
        Integer stockCountSurplus = activitySkuEntity.getStockCountSurplus();
        log.info(String.valueOf(stockCountSurplus));
        // 校验；活动sku库存 「剩余库存从缓存获取的」
        if (stockCountSurplus <= 0) {
            throw new AppException(ResponseCode.ACTIVITY_SKU_STOCK_ERROR.getCode(), ResponseCode.ACTIVITY_SKU_STOCK_ERROR.getInfo());
        }
        return next().action(activitySkuEntity, activityCountEntity, activityEntity);
    }
}
